package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import com.x.erp.model.City;
import org.springframework.stereotype.Repository;

import com.x.erp.model.Province;
import com.x.framework.dao.BaseDao;

@Repository
public class ProvinceDao extends BaseDao {
    private final static String selectProvinceListSql = "SELECT * FROM BASE_PROVINCE WHERE PROVINCE_STATE = 1 ";
    public List<Province> selectProvinceList(Province province) throws Exception {
    	return selectProvince(province);
    }
    public List<Province> selectProvince(Province province) throws Exception{
    	StringBuilder sb = new StringBuilder(selectProvinceListSql);
    	List<Object> params = new ArrayList<Object>();
    	List<Province> result = new ArrayList<Province>();
    	if(this.isString(province.getProvinceId())){
    		sb.append("AND PROVINCE_ID = ? ");
    		params.add(province.getProvinceId());
    	}
    	if(this.isString(province.getProvinceCode())){
    		sb.append("AND PROVINCE_CODE = ? ");
    		params.add(province.getProvinceCode());
    	}
    	if(this.isString(province.getProvinceName())){
    		sb.append("AND PROVINCE_NAME = ? ");
    		params.add(province.getProvinceName());
    	}
    	if(province.getProvinceSeq() != null){
    		sb.append("AND PROVINCE_SEQ = ? ");
    		params.add(province.getProvinceSeq());
    	}
    	if(this.isString(province.getProvincePinyin())){
    		sb.append("AND PROVINCE_PINYIN LIKE ? ");
    		params.add(province.getProvincePinyin()+"%");
    	}
    	if(this.isString(province.getProvinceInitial())){
    		sb.append("AND PROVINCE_INITIAL LIKE ? ");
    		params.add(province.getProvinceInitial()+"%");
    	}
    	if(province.getProvinceState() != null){
    		sb.append("AND PROVINCE_STATE = ? ");
    		params.add(province.getProvinceState());
    	}
    	result = this.queryForList(sb.toString(), Province.class, params);
    	return result;
    }

	private final static String selectCooperationProvinceListSql = "SELECT " +
			"DISTINCT P.* FROM BASE_CITY C " +
			"JOIN BASE_PROVINCE P ON C.PROVINCE_ID = P.PROVINCE_ID " +
			"JOIN ERP_MARKET M ON C.CITY_ID = M.CITY_ID " +
			"WHERE 1 = 1 ";

	/**
	 * 获取启辕合作市场的省份列表
	 * @param province
	 * @return
	 * @throws Exception
	 */
	public List<Province> selectCooperationProvinceList(Province province) throws Exception {
		StringBuilder sb = new StringBuilder (selectCooperationProvinceListSql);
		List<Object> params = new ArrayList<> ();
		return this.queryForList (sb.toString (), Province.class, params);
	}
}
